<?php

namespace App\Models\Common\English;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class EnCollPosition extends Model{
    protected $table = 'coll_position';
    //设置主键值
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $fillable = [
        'cid', 'en_name','grade','pid','sort'
    ];

    /**
     * $par  $id 当前选项ID
     * 获取当前选项职位自己ID
     * @return array
     */
    public static function CurrPosition($id){
        $currArr = self::find($id)->toArray();
        $arr = array();
        if($currArr){
            if($currArr['grade'] == 1){
                $arr[] = $currArr['id'];
                //当前二级职位
                $twoPosition = self::where('grade', 2)->where('pid', $currArr['cid'])->orderBy('cid','asc')->get();
                $twoPosition = setSort($twoPosition);
                foreach ($twoPosition as $k => $v) {
                    $arr[] = $v['id'];
                    //当前三级职位
                    $threePosition = self::where('grade', 3)->where('pid', $v->cid)->orderBy('cid','asc')->get();
                    $threePosition = setSort($threePosition);
                    foreach($threePosition as $vv){
                        $arr[] = $vv['id'];
                    }
                }
                array_push($arr,$currArr['id']);
            }else if($currArr['grade'] == 2){
                $arr = array_column( self::where('pid',$currArr['cid'])->get()->toArray(),'id');
                array_push($arr,$currArr['id']);
            }else{
                $arr[] = $currArr['id'];
            };
            return $arr;
        }else{
            return array();
        }
    }

//-----------------------------------------------------------------------------------------------------------------------
    /**
     * 提取一级职位
     * @return array
     */
    public static function getFirstPosition(){
        $position=self::where('grade', 1)->get();
        $position = setSort($position);
        $positionArray=array();
        foreach($position as $key=>$value){
            $positionArray[]=array('value'=>$value['en_name'],'text'=>$value['cid']);
        }
        return $positionArray;
    }

    /**
     * 提取二级职位
     * @return array
     * $pid:一级职位的id
     */
    public static function getSecondPosition($pid){
        $position=self::where('grade', 2)->where('pid',$pid)->get();
        $position = setSort($position);
        $positionArray=array();
        foreach($position as $key=>$value){
            $positionArray[]=array('value'=>$value['en_name'],'text'=>$value['cid']);
        }
        return $positionArray;
    }

    /**
     * 提取三级职位
     * @return array
     * $pid:二级职位的id
     */
    public static function getThirdPosition($pid){
        $position=self::where('grade', 3)->where('pid',$pid)->get();
        $position = setSort($position);
        $positionArray=array();
        foreach($position as $key=>$value){
            $positionArray[]=array('value'=>$value['en_name'],'text'=>$value['cid']);
        }
        return $positionArray;
    }

    /**
     * 提取一级和二级职位//二维
     * @return array
     */
    public static function getAllTwo(){
        $allTwoArray=array();
        if (Cache::has('allTwoPositions')) {
            $allTwoArray= Cache::get('allTwoPositions');
        }else {
            $firstPosition = self::where('grade', 1)->get();
            $firstPosition = setSort($firstPosition);
            foreach ($firstPosition as $key => $value) {
                $secondPosition = self::where('grade', 2)->where('pid', $value['cid'])->get();
                $secondPosition = setSort($secondPosition);
                $secondPositionArray = array();
                foreach ($secondPosition as $k => $v) {
                    $secondPositionArray[] = array('value' => $v['en_name'], 'text' => $v['cid']);
                }
                $allTwoArray[] = array('value' => $value['en_name'], 'text' => $value['cid'], 'secondPosition' => $secondPositionArray);
            }
            Cache::forever('allTwoPositions', $allTwoArray);
        }
        return $allTwoArray;
    }


    /**
     * 提取一级、二级、三级职位//三维
     * @return array
     */
    public static function getAllThree(){
        $allThreeArray=array();
        if (Cache::has('allThreePositions')) {
            $allThreeArray= Cache::get('allThreePositions');
        }else{
            $firstPosition=self::where('grade', 1)->get();
            $firstPosition = setSort($firstPosition);

            foreach($firstPosition as $key=>$value){
                $secondPosition=self::where('grade', 2)->where('pid',$value['cid'])->get();
                $secondPosition = setSort($secondPosition);

                $secondPositionArray=array();
                foreach($secondPosition as $k=>$v) {
                    $thirdPosition=self::where('grade', 3)->where('pid',$v['cid'])->get();
                    $thirdPosition = setSort($thirdPosition);

                    $thirdPositionArray=array();
                    foreach($thirdPosition as $third_k=>$third_v){
                        $thirdPositionArray[]=array('value'=>$third_v['en_name'],'text'=>$third_v['cid']);
                    }
                    $secondPositionArray[]=array('value'=>$v['en_name'],'text'=>$v['cid'],'thirdPositions'=>$thirdPositionArray);
                }
                $allThreeArray[]=array('value'=>$value['en_name'],'text'=>$value['cid'],'secondPosition'=>$secondPositionArray);
            }
            Cache::forever('allThreePositions', $allThreeArray);
        }
        return $allThreeArray;
    }

    /**
     * 清理本模型中的所有缓存数据
     */
    public static function clearCache(){
        Cache::forget('allTwoPositions');
        Cache::forget('allThreePositions');
        return '';
    }

    /**
     * 提取搜索的三级信息
     * $keyword 搜索的值
     */
    public static function getSearchThree($keyword){
        $arr = [];
        //获取三级职位
        $thirdPosition=self::where('grade', 3)->get()->toArray();
        $thirdPosition = setSort($thirdPosition);
        foreach($thirdPosition as $v) {
            $num = cnsimilar_text($v['en_name'] ,$keyword);
            if(cnsimilar_text($v['en_name'] ,$keyword) != 0){
                $arr[] = ['en_name'=>$v['en_name'],'cid'=>$v['cid'],'pid'=>$v['pid'],'num'=>$num];
            } ;
        }
        if(count($arr) > 0){
            $newArr=array();
            for($j=0;$j<count($arr);$j++){
                $newArr[]=$arr[$j]['num'];
            }
            //根据相似度的值倒序排序
            array_multisort($newArr,SORT_DESC,$arr);
            $arr = array_slice($arr,0,10);
        }
        return $arr;
    }

    /**
     * $cid:选项编号
     * @return： 选项名称
     */
    public static function getPosName($cid){
        $city= self::where('cid',$cid)->first();
        if($city){
            return $city->en_name;
        }
        return'';
    }

}
